<template>
    <div>
        <div class="bg"></div>
        <div class="main_body">
            <div class="title">
                <img class="title_img" src="../assets/img/title.png" alt="">
            </div>
            <div>
                <el-tabs v-model="activeName">
                    <el-tab-pane label="账号密码登录" name="login"></el-tab-pane>
                </el-tabs>
            </div>
            <div>
                <el-form status-icon ref="loginFormRef" :rules="rules" :model="loginForm">
                    <el-form-item prop="username">
                        <el-input prefix-icon="el-icon-user" placeholder="账号名" v-model="loginForm.username"></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input @keyup.enter.native="login" type="password" autocomplete="false" placeholder="密码" prefix-icon="el-icon-lock" v-model="loginForm.password"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <div style="float: right">
                            <el-link  style="margin-right: 10px;font-size: 12px;" type="primary" @click="ToRegister" :underline="false">注册账号</el-link>
                            <el-link  style="font-size: 12px;" type="primary"  :underline="false">忘记密码</el-link>
                        </div>
                    </el-form-item>
                    <el-form-item>
                        <el-button  class="btnSure" type="primary" @click="login">确 定</el-button>
                    </el-form-item>
                </el-form>
                <p style="font-size: 12px;color: #0a3d62">
                    其他登录方式
                </p>
            </div>
            <div class="copy">
                right © 2021 MoerTie2
                <br>
                浙ICP备20025128号-1
            </div>
        </div>
    </div>
</template>

<script>
    var md5 = require('md5')
    export default {
        data(){
            return {
                activeName: 'login',
                loginForm: {
                    username: '',
                    password: ''
                },
                rules: {
                    username: [
                        { required: true, message: '请输入帐户名', trigger: 'blur' },
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                    ],
                }
            }
        },
        created() {
            this.getUser()
        },
        methods: {
            getUser(){
                // console.log(this.$route.query.username)
                this.loginForm.username = this.$route.query.username ? this.$route.query.username : ''
            },
            async login(){
                if(this.loginForm.password&&this.loginForm.username){
                    var pass = this.loginForm.password
                    this.loginForm.password = md5(pass)
                    console.log(this.loginForm)
                    const { data: res } = await this.$http.post('/user/login',this.loginForm)
                    console.log(res)
                    if (res.code !== 200){
                        return this.$message.error(res.msg)
                    }
                    // console.log(res.data.userId,res.data.nickname)
                    localStorage.setItem('username', res.data.username)
                    localStorage.setItem('nickname', res.data.nickname)
                    localStorage.setItem('perm',res.data.perm)
                    localStorage.setItem('IsLogin',true)
                    if (res.data.perm == 'user') {
                        this.$message.warning('欢迎来到摩尔贴帖~')
                        this.$router.push('/user/Main')
                    } else {
                        this.$message.success(res.data.nickname +'，后台管理欢迎您！')
                        this.$router.push('/admin/Main')
                    }

                }
                else{
                    this.$message.error('登录信息填写不完整!')
                }
            },
            ToRegister(){
                this.$router.push('/Register')
            }
        }
    };
</script>


<style scoped>
    .bg{
        background: url(../assets/img/bg2.jpg);
        /*background-color: #F7F7F7;*/
        background-size: 100% 100%;
        height: 100%;
        opacity: 0.2;
        position: fixed;
        width: 100%;
        background-size: cover;
        -webkit-background-size: cover;
        -moz-background-size: cover;
        -o-background-size: cover;
    }
    .main_body{
        display: flex;
        align-items: center;
        flex-direction: column;
    }
    .title_img{
        width: 192px;
        height: 64px;
    }
    .title{
        margin-top: 10vh;
    }
    /deep/.el-tabs__item {
        font-size: 11px;
    }
    /deep/.el-input__inner{
        width: 184px !important;
        height: 24px;
    }
    .btnSure{
        width: 184px !important;
        height: 24px;
    }
    .copy{
        color: rgba(0,0,0,.45);
        font-size: 6px;
        position: absolute;
        bottom: 20px;
        text-align: center;
    }
    /deep/.el-tabs__item.is-active {
        color: #26386a !important;
        font-size: 9px !important;
    }
    /deep/.el-tabs__item:hover {
        color: #26386a !important;
        cursor: pointer;
        font-size: 9px !important;
    }
</style>
